﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Model;
using Model.Entities;
using MusicSite.ViewClasses;

namespace MusicSite.Views.Content.Artist
{
    public partial class EditArt : BasePage
    {
        private ArtistProvider artProv = new ArtistProvider();
        private ArtistDetails editArt = new ArtistDetails();
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckAuthenticationEditPage();
            string artId = Request.QueryString["ArtistId"];
            if (string.IsNullOrEmpty(artId))
            {
                btnDelete.Visible = false;
                btnUpdate.Text = "Create";
                return;
            }
            editArt = artProv.GetArtistById(Convert.ToInt32(artId));
            if (!IsPostBack)
            {
                artistName.Text = editArt.ArtistName;
                description.Text = editArt.Description;
                photo.ImageUrl = "~/Content/images/" + editArt.Photo;
                foundationDate.Text = editArt.FoundationDate.Value.ToShortDateString();
            }
        }

        protected void UpdateArtist(object sender, EventArgs e)
        {
            if (IsValid)
            {
                editArt.ArtistName = artistName.Text;
                editArt.Description = description.Text;
                if (!string.IsNullOrEmpty(fileUpload.FileName))
                    editArt.Photo = Guid.NewGuid() + fileUpload.FileName;
                editArt.FoundationDate = DateTime.Parse(foundationDate.Text);
                SaveImageOnDisc(fileUpload);
                if (btnUpdate.Text == "Create")
                    artProv.InsertArtist(editArt);
                else
                    artProv.UpdateArtist(editArt);
                Response.Redirect("~/Views/Content/Artist/IndexArt.aspx");
            }
        }

        protected void DeleteArtist(object sender, EventArgs e)
        {
            artProv.DeleteArtist(editArt);
            Response.Redirect("~/Views/Content/Artist/IndexArt.aspx");
        }

        public void SaveImageOnDisc(FileUpload img)
        {
            if (string.IsNullOrEmpty(img.FileName) || img.FileBytes.Length == 0)
                return;
            string dir = Server.MapPath("~/Content/images/" + editArt.Photo);
            img.SaveAs(dir);
        }

        protected void DateValidate(object sender, ServerValidateEventArgs args)
        {
            DateTime date = new DateTime();
            DateTime.TryParse(args.Value, out date);
            if ((date < new DateTime(2009, 12, 31)) && (date > new DateTime(1900, 1, 1)))
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    }
}
